<?php

/**
 * @Author: king
 * @Date:   2019-05-04 16:58:19
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-26 16:36:38
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;
class LanmeiCounselorExerciseType extends Base
{
    const CounselorExerciseType = 'counselor:exercise:type';
    const TAG = 'counselor_exercise';//获取咨询师习题类型
    const CACHE_TIME = 86400 * 7;
    protected $table = 'counselor_exercise_type';

    /**
     * 获取习题类型信息
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getUserExerciseType()
    {
        $data = false;

        if ($this->cache_on >= 1 && Cache::has(self::CounselorExerciseType)) {
            $data = Cache::get(self::CounselorExerciseType);
        }

        if ($data === false) {
            $where = [
                'status' => 1
            ];

            $field = ['id','name','img','description','time','num'];
            try {

                $data = $this->select($field)->where($where)->orderBy('sort', 'DESC')->get();
                $data = json_decode(json_encode($data, true), true);
                if ($this->cache_on >= 1) {
                    Cache::tags(self::TAG)->set(self::CounselorExerciseType, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
               throw new ApiException('获取习题类型失败', config('code.error'));
            }
        }
        return $data;
    }
}
